package model;

import java.sql.Timestamp;
import dao.UserDAO;

import java.util.List;

public class User {
	
	private UserDAO userDAO;

	private String username;
	private String password;
	private String address;
	private String firstName;
	private String lastName;
	private String phone;
	private Timestamp created; //fecha de creacion
	private Timestamp modified; //fecha de la ultima modificacion
	private int active;
	
	public User () {
		userDAO = new UserDAO();
	}
	
	public String getUsername(){return this.username;}
	public String getPassword(){return this.password;}
	public String getAddress(){return this.address;}
	public String getFirstName(){return this.firstName;}
	public String getLastName(){return this.lastName;}
	public String getPhone(){return this.phone;}
	public Timestamp getCreated(){return this.created;}
	public Timestamp getModified(){return this.modified;}
	public int getActive(){return this.active;}
	
	public void setUsername(String user){this.username = user;}
	public void setPassword(String pass){this.password = pass;}
	public void setAddress(String address){this.address = address;}
	public void setFirstName(String firstName){this.firstName = firstName;}
	public void setLastName(String lastName){this.lastName = lastName;}
	public void setPhone(String phone){this.phone = phone;}
	public void setCreated(Timestamp created){this.created = created;}
	public void setModified(Timestamp modified){this.modified = modified;}
	public void setActive(int active){this.active = active;}
	
	
	public List<User> getUsers() {
		return userDAO.getAll();
	}
	
	public boolean login(String username, String password) {
		User temp = userDAO.select(username);
		if( temp != null ) {
			if (password.equals(temp.getPassword()))
				return true;
		}
		return false;
	}
	
	public String getName(String username){
		User temp = userDAO.select(username);
		return temp.getFirstName()+" "+temp.getLastName();
	}
	
	public int register(String username, String password, String firstname, String lastname, String address, String phone) {
		User temp = new User();
		java.util.Date today = new java.util.Date();
	    java.sql.Timestamp now = new java.sql.Timestamp(today.getTime());
		
		temp.setUsername(username);
		temp.setPassword(password);
		temp.setFirstName(firstname);
		temp.setLastName(lastname);
		temp.setAddress(address);
		temp.setPhone(phone);
		temp.setCreated(now);
		temp.setModified(now);
		temp.setActive(1);
		
		try{
			userDAO.create(temp);
			return 1;
		}
		catch(Exception e){
			return 0;
		}
	}
	
	public int updateData(String username, String firstname, String lastname, String address, String phone){
		User temp = new User();
		java.util.Date today = new java.util.Date();
	    java.sql.Timestamp now = new java.sql.Timestamp(today.getTime());
	    
		temp = userDAO.select(username);
		temp.setFirstName(firstname);
		temp.setLastName(lastname);
		temp.setAddress(address);
		temp.setPhone(phone);
		temp.setModified(now);
		
		try{
			userDAO.update(temp);
			return 1;
		}
		catch(Exception e){
			return 0;
		}
	}
	
	public int changePassword(String username, String new_password){
		User temp = new User();
		java.util.Date today = new java.util.Date();
	    java.sql.Timestamp now = new java.sql.Timestamp(today.getTime());
	    
	    temp = userDAO.select(username);
		temp.setPassword(new_password);
		temp.setModified(now);			
		try{
			userDAO.update(temp);
			return 1;
		}
		catch(Exception e){
			return 0;
		}
	}
	
}
